package com.hyf.popupdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.view.menu.ActionMenuItemView;
import android.support.v7.widget.ActionMenuView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.hyf.popupdemo.activity.PopupMenuActivity;
import com.hyf.popupdemo.activity.TestAnyweherShowPopActivity;
import com.hyf.popupdemo.base.BaseActivity;
import com.hyf.popupdemo.support.PopWindowMenuHelper;
import com.hyf.popupdemo.support.PopupWindowMenu;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private Button popMenu;
    private Button btnFourLocTest;

    private List<PopupWindowMenu> list = new ArrayList<>();
    private Activity mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        toolBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.more) {
//                    new SnackTopToast(MainActivity.this)
//                            .setIconRes(R.drawable.hyf_ic_download_success)
//                            .setBackgroundColor(Color.parseColor("#09bb07"))
//                            .setTitle("下载成功")
//                            .setTitleColor(Color.WHITE)
//                            .setIconColor(Color.WHITE)
//                            .show();

                    new PopWindowMenuHelper.TopBuilder(mContext, list, getToolbarActionMenuItemView(toolBar, R.id.more))
                            .setShowTriangle(true)
                            .setIsBlur(true)
                            .setOnPopItemClickListener(menuItemClickListener).show();
                    return true;
                }
                return false;
            }
        });

        // 第一个按钮
        popMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new PopWindowMenuHelper.BottomBuilder(mContext, list)
                        .setIsBlur(true)
                        .setOnPopItemClickListener(menuItemClickListener).show();
            }
        });

        btnFourLocTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, PopupMenuActivity.class));
            }
        });

        findViewById(R.id.btn_pop_anywhere_loc_test)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(getContext(), TestAnyweherShowPopActivity.class));
                    }
                });
    }

    private PopWindowMenuHelper.OnItemClickListener menuItemClickListener = new PopWindowMenuHelper.OnItemClickListener() {
        @Override
        public void onClickPopwindowMenuItem(View view, PopupWindow window, int position) {
            Toast.makeText(mContext, list.get(position).getTitle(), Toast.LENGTH_SHORT).show();
            window.dismiss();
        }
    };

    private void initData() {
        list.addAll(DataSource.getMenuList());
    }

    private void initView() {
        popMenu = (Button) findViewById(R.id.btn_pop_menu);
        btnFourLocTest = (Button) findViewById(R.id.btn_pop_four_loc_test);

        toolBar.inflateMenu(R.menu.more);
    }

    // 获取ActionMenuView
    private ActionMenuItemView getToolbarActionMenuItemView(Toolbar toolbar, int itemId) {
        ActionMenuItemView itemView = null;
        for (int i = 0; i < toolbar.getChildCount(); i++) {
            View view = toolbar.getChildAt(i);
            if (view instanceof ActionMenuView) {
                ActionMenuView actionMenuView = (ActionMenuView) view;
                for (int j = 0; j < actionMenuView.getChildCount(); j++) {
                    ActionMenuItemView item = (ActionMenuItemView) actionMenuView.getChildAt(j);
                    if (item.getId() == itemId) {
                        itemView = item;
                        break;
                    }
                }
                break;
            }
        }

        return itemView;
    }
}
